<article id="wikiArticle">
<p></p><p></p>
<p><strong><code>repeat()</code></strong> 构造并返回一个新字符串，该字符串包含被连接在一起的指定数量的字符串的副本。</p>
<h2 id="Syntax" name="Syntax">语法</h2>
<pre><code class="language-javascript"><code><var>/** 
 * str: String
 * count: Number
 */

let resultString = str</var>.repeat(<var>count</var>);</code></code></pre>
<h3 id="Parameters" name="Parameters">参数</h3>
<dl>
<dt><code>count</code></dt>
<dd>介于0和正无穷大之间的整数 : [0, +∞) 。表示在新构造的字符串中重复了多少遍原字符串。</dd>
</dl>
<h3 id="返回值">返回值</h3>
<p> 包含指定字符串的指定数量副本的新字符串。</p>
<h3 id="Exceptions">Exceptions</h3>
<ul>
<li><a href="Reference/Errors/Negative_repetition_count" title="RangeError"><code>RangeError</code></a>: 重复次数不能为负数。</li>
<li><a href="Reference/Errors/Resulting_string_too_large" title="RangeError"><code>RangeError</code></a>: 重复次数必须小于 infinity，且长度不会大于最长的字符串。</li>
</ul>
<h2 id="Examples" name="Examples">示例</h2>
<pre><code class="language-js"><span class="difflineplus">"abc".repeat(-1)     // </span>RangeError: repeat count must be positive and less than inifinity
<span class="difflineplus">"abc".repeat(0)      // ""</span>
<span class="difflineplus">"abc".repeat(1)      // "abc"</span>
<span class="difflineplus">"abc".repeat(2)      // "</span>abcabc<span class="difflineplus">"
</span>"abc".repeat(3.5)<span class="difflineplus">    // "abcabcabc" 参数count将会被自动转换成整数.
</span><span class="difflineplus">"abc".repeat(1/0)    //</span> RangeError: repeat count must be positive and less than inifinity

<span class="difflineplus">({toString : () =&gt; "abc", repeat : String.prototype.repeat}</span><span class="difflineplus">).repeat(2)   
</span><span class="difflineplus">//"</span>abcabc<span class="difflineplus">",repeat是一个通用方法</span>,也就是它的调用者可以不是一个字符串对象.</code></pre>
<h2 id="填充">填充</h2>
<p>此方法已添加到ECMAScript 2015规范中，并且可能尚未在所有JavaScript实现中可用。然而，你可以使用以下代码段对 String.prototype.repeat() 进行填充：</p>
<pre><code class="language-js language-js"><code class="language-js"><span class="keyword token">if</span> <span class="punctuation token">(</span><span class="operator token">!</span>String<span class="punctuation token">.</span>prototype<span class="punctuation token">.</span>repeat<span class="punctuation token">)</span> <span class="punctuation token">{</span>
  String<span class="punctuation token">.</span>prototype<span class="punctuation token">.</span>repeat <span class="operator token">=</span> <span class="keyword token">function</span><span class="punctuation token">(</span>count<span class="punctuation token">)</span> <span class="punctuation token">{</span>
    <span class="string token">'use strict'</span><span class="punctuation token">;</span>
    <span class="keyword token">if</span> <span class="punctuation token">(</span><span class="keyword token">this</span> <span class="operator token">==</span> <span class="keyword token">null</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
      <span class="keyword token">throw</span> <span class="keyword token">new</span> <span class="class-name token">TypeError</span><span class="punctuation token">(</span><span class="string token">'can\'t convert '</span> <span class="operator token">+</span> <span class="keyword token">this</span> <span class="operator token">+</span> <span class="string token">' to object'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
    <span class="punctuation token">}</span>
    <span class="keyword token">var</span> str <span class="operator token">=</span> <span class="string token">''</span> <span class="operator token">+</span> <span class="keyword token">this</span><span class="punctuation token">;</span>
    count <span class="operator token">=</span> <span class="operator token">+</span>count<span class="punctuation token">;</span>
    <span class="keyword token">if</span> <span class="punctuation token">(</span>count <span class="operator token">!</span><span class="operator token">=</span> count<span class="punctuation token">)</span> <span class="punctuation token">{</span>
      count <span class="operator token">=</span> <span class="number token">0</span><span class="punctuation token">;</span>
    <span class="punctuation token">}</span>
    <span class="keyword token">if</span> <span class="punctuation token">(</span>count <span class="operator token">&lt;</span> <span class="number token">0</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
      <span class="keyword token">throw</span> <span class="keyword token">new</span> <span class="class-name token">RangeError</span><span class="punctuation token">(</span><span class="string token">'repeat count must be non-negative'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
    <span class="punctuation token">}</span>
    <span class="keyword token">if</span> <span class="punctuation token">(</span>count <span class="operator token">==</span> <span class="number token">Infinity</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
      <span class="keyword token">throw</span> <span class="keyword token">new</span> <span class="class-name token">RangeError</span><span class="punctuation token">(</span><span class="string token">'repeat count must be less than infinity'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
    <span class="punctuation token">}</span>
    count <span class="operator token">=</span> Math<span class="punctuation token">.</span><span class="function token">floor<span class="punctuation token">(</span></span>count<span class="punctuation token">)</span><span class="punctuation token">;</span>
    <span class="keyword token">if</span> <span class="punctuation token">(</span>str<span class="punctuation token">.</span>length <span class="operator token">==</span> <span class="number token">0</span> <span class="operator token">||</span> count <span class="operator token">==</span> <span class="number token">0</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
      <span class="keyword token">return</span> <span class="string token">''</span><span class="punctuation token">;</span>
    <span class="punctuation token">}</span>
   <span class="comment token"> // 确保 count 是一个 31 位的整数。这样我们就可以使用如下优化的算法。
    // 当前（2014年8月），绝大多数浏览器都不能支持 1 &lt;&lt; 28 长的字符串，所以：</span><span class="comment token">
</span>    <span class="keyword token">if</span> <span class="punctuation token">(</span>str<span class="punctuation token">.</span>length <span class="operator token">*</span> count <span class="operator token">&gt;=</span> <span class="number token">1</span> <span class="operator token">&lt;</span><span class="operator token">&lt;</span> <span class="number token">28</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
      <span class="keyword token">throw</span> <span class="keyword token">new</span> <span class="class-name token">RangeError</span><span class="punctuation token">(</span><span class="string token">'repeat count must not overflow maximum string size'</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
    <span class="punctuation token">}</span>
    <span class="keyword token">var</span> rpt <span class="operator token">=</span> <span class="string token">''</span><span class="punctuation token">;</span>
    <span class="keyword token">for</span> <span class="punctuation token">(</span><span class="punctuation token">;</span><span class="punctuation token">;</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
      <span class="keyword token">if</span> <span class="punctuation token">(</span><span class="punctuation token">(</span>count <span class="operator token">&amp;</span> <span class="number token">1</span><span class="punctuation token">)</span> <span class="operator token">==</span> <span class="number token">1</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
        rpt <span class="operator token">+</span><span class="operator token">=</span> str<span class="punctuation token">;</span>
      <span class="punctuation token">}</span>
      count <span class="operator token">&gt;</span><span class="operator token">&gt;</span><span class="operator token">&gt;=</span> <span class="number token">1</span><span class="punctuation token">;</span>
      <span class="keyword token">if</span> <span class="punctuation token">(</span>count <span class="operator token">==</span> <span class="number token">0</span><span class="punctuation token">)</span> <span class="punctuation token">{</span>
        <span class="keyword token">break</span><span class="punctuation token">;</span>
      <span class="punctuation token">}</span>
      str <span class="operator token">+</span><span class="operator token">=</span> str<span class="punctuation token">;</span>
    <span class="punctuation token">}</span>
    <span class="keyword token">return</span> rpt<span class="punctuation token">;</span>
  <span class="punctuation token">}</span>
<span class="punctuation token">}</span></code></code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-string.prototype.repeat" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">String.prototype.repeat</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>Initial definition.</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-string.prototype.repeat" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">String.prototype.repeat</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility"> </h2>
<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>
<div><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
            This compatibility table still uses the old format,
            because we haven't yet converted the data it contains.
            <strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
<div class="htab">
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
<ul>
<li class="selected"><a>Desktop</a></li>
<li><a>Mobile</a></li>
</ul>
</div></div>
<div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>41 </td>
<td><a href="/en-US/Firefox/Releases/24" title="Released on 2013-09-17.">24</a> (24)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td>9</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Chrome for Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td><span style="color: #f00;">未实现</span></td>
<td>36 (behind a flag)</td>
<td>24.0 (24)</td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
<td><span style="color: #f00;">未实现</span></td>
</tr>
</tbody>
</table>
</div>
<p> </p>
</div>
</article>